from termios import CWERASE
from django.db import models
from django.utils import timezone
from matplotlib.pyplot import cla
from utils.model import BaseModel
from django.contrib.auth import get_user_model

User = get_user_model()


# 本地单
class LocalOrder(BaseModel):    
    # 物流方式选项
    SHIPPING_METHOD_CHOICES = [
        ('SAGAWA', '佐川'),
        ('YAMATO', '黑猫'),
    ]

    # 基本信息
    fs_no = models.CharField('FS编号', max_length=40, unique=True, default='')
    company_name = models.CharField('公司名称', max_length=255, default='', blank=True, null=True)
    address1 = models.CharField('地址1', max_length=255, default='', blank=True, null=True)
    address2 = models.CharField('地址2', max_length=255, default='', blank=True, null=True)
    phone_number = models.CharField('电话号码', max_length=20, default='', blank=True, null=True)
    attention = models.CharField('收件人', max_length=255, default='', blank=True, null=True)
    country = models.CharField('国家/地区', max_length=50, default='japan', blank=True, null=True)
    postal_code = models.CharField('邮编', max_length=10, default='')
    city = models.CharField('城市', max_length=255, default='', blank=True, null=True)
    state = models.CharField('州/县', max_length=255, default='', blank=True, null=True)
    is_changecode = models.BooleanField('是否改码', default=False)
    changecode_id_count = models.CharField('改码id数', max_length=50, default='', blank=True, null=True)
    box_count = models.IntegerField('箱数', default=1)
    is_pickup = models.BooleanField('是否自提', default=False)
    pickup_time = models.DateTimeField('自提时间', blank=True, null=True)
    remark = models.TextField('备注', blank=True, null=True, default='')
    remark_status = models.BooleanField('备注状态', default=False)

    # 包裹信息
    weight = models.DecimalField('重量(kg)', max_digits=10, decimal_places=2)
    length = models.DecimalField('长度(cm)', max_digits=10, decimal_places=2)
    width = models.DecimalField('宽度(cm)', max_digits=10, decimal_places=2)
    height = models.DecimalField('高度(cm)', max_digits=10, decimal_places=2)
    
    # 物流信息
    shipping_method = models.CharField(
        '物流方式',
        max_length=20,
        choices=SHIPPING_METHOD_CHOICES,
        default='',
        blank=True,
        null=True
    )
    tracking_number = models.CharField(
        '物流号',
        max_length=50,
        blank=True,
        null=True,
        default=''
    )
    yamato_price = models.CharField('黑猫运费', max_length=5, blank=True, null=True, default='')
    sagawa_price = models.CharField('佐川运费', max_length=5, blank=True, null=True, default='')
    
    # 物流员
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='创建人',
        related_name='local_orders',
        null=True,
        blank=True
    )
    
    class Meta:
        verbose_name = '本地订单'
        verbose_name_plural = '本地订单'
        ordering = ['-create_time']
    
    def __str__(self):
        return f"{self.fs_no}"
    
    
    def calculate_volume(self):
        """计算体积"""
        return self.length + self.width + self.height


# 转运单
class TransferOrder(BaseModel):
    # 物流方式选项
    SHIPPING_METHOD_CHOICES = [
        ('SAGAWA', '佐川'),
        ('YAMATO', '黑猫'),
    ]

    # 基本信息
    fs_no = models.CharField('FS编号', max_length=40, default='', blank=True, null=True)
    cw_no = models.CharField('CW编号', max_length=40, unique=True, default='')
    company_name = models.CharField('公司名称', max_length=255, default='', blank=True, null=True)
    address1 = models.CharField('地址1', max_length=255, default='', blank=True, null=True)
    address2 = models.CharField('地址2', max_length=255, blank=True, null=True, default='')
    phone_number = models.CharField('电话号码', max_length=20, default='', blank=True, null=True)
    attention = models.CharField('收件人', max_length=255, default='', blank=True, null=True)
    country = models.CharField('国家/地区', max_length=50, default='japan', blank=True, null=True)
    postal_code = models.CharField('邮编', max_length=10, default='')
    city = models.CharField('城市', max_length=255, default='', blank=True, null=True)
    state = models.CharField('州/县', max_length=255, default='', blank=True, null=True)
    box_count = models.IntegerField('箱数', default=1)
    is_pickup = models.BooleanField('是否自提', default=False)
    pickup_time = models.DateTimeField('自提时间', blank=True, null=True)
    remark = models.TextField('备注', blank=True, null=True, default='')
    remark_status = models.BooleanField('备注状态', default=False)
    # 包裹信息
    weight = models.DecimalField('重量(kg)', max_digits=10, decimal_places=2)
    length = models.DecimalField('长度(cm)', max_digits=10, decimal_places=2)
    width = models.DecimalField('宽度(cm)', max_digits=10, decimal_places=2)
    height = models.DecimalField('高度(cm)', max_digits=10, decimal_places=2)

    # 物流信息
    shipping_method = models.CharField(
        '物流方式',
        max_length=20,
        choices=SHIPPING_METHOD_CHOICES,
        default='',
        blank=True,
        null=True
    )
    tracking_number = models.CharField(
        '物流号',
        max_length=50,
        blank=True,
        null=True,
        default=''
    )
    yamato_price = models.CharField('黑猫运费', max_length=5, blank=True, null=True, default='')
    sagawa_price = models.CharField('佐川运费', max_length=5, blank=True, null=True, default='')
    
    # 物流员
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='transfer_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '转运订单'
        verbose_name_plural = '转运订单'
        ordering = ['-create_time']

    def __str__(self):
        return f"{self.cw_no}"
    
    def calculate_volume(self):
        """计算体积"""
        return self.length + self.width + self.height


# 售后单/退换货单
class AfterSaleOrder(BaseModel):
    PROCESS_CHOICES = [
        ('IN_PRODUCT', '入成品仓'),
        ('REPAIR', '回国维修'),
        ('SCRAP', '报废'),
    ]
    cs_no = models.CharField('CS编号', max_length=40)
    fs_no = models.CharField('FS编号', max_length=40)
    cw_no = models.CharField('CW编号', max_length=40)
    supplier = models.CharField('供应商', max_length=40, default='', blank=True, null=True)
    cg_no = models.CharField('CG编号', max_length=40, default='', blank=True, null=True)
    box_count = models.IntegerField('箱数', default=1)
    id_count = models.IntegerField('id数', default=1)
    process = models.CharField('处理方式', max_length=40, choices=PROCESS_CHOICES, default='', blank=True, null=True)
    product_id = models.CharField('产品ID', max_length=200, default='', blank=True, null=True)
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='aftersale_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '售后订单'
        verbose_name_plural = '售后订单'
        ordering = ['-create_time']
    
    def __str__(self):
        return f"{self.cs_no}"
    

# 调仓单
class RelocationOrder(BaseModel):
    country = models.CharField('国家', max_length=50, default='', blank=True, null=True)
    carrier = models.CharField('承运商', max_length=40, default='', blank=True, null=True)
    carrier_no = models.CharField('物流号', max_length=40, default='', blank=True, null=True)
    order_count = models.IntegerField('订单数', default=1)
    box_count = models.IntegerField('箱数', default=1)
    id_count = models.IntegerField('id数', default=1)
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='relocation_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '调仓单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']


# 跨仓单
class CrossOrder(BaseModel):
    order_count = models.IntegerField('订单数', default=1)
    box_count = models.IntegerField('箱数', default=1)
    id_count = models.IntegerField('id数', default=1)
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='cross_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '跨仓单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']


# 包裹回国
class ReturnOrder(BaseModel):
    rt_no = models.CharField('RT编号', max_length=40)
    cs_no = models.CharField('CS编号', max_length=40)
    carrier = models.CharField('承运商', max_length=40, default='DHL', blank=True, null=True)
    carrier_no = models.CharField('物流号', max_length=40, default='', blank=True, null=True)
    fee = models.DecimalField('运费', max_digits=10, decimal_places=2, default=0)
    order_count = models.IntegerField('订单数', default=1)
    box_count = models.IntegerField('箱数', default=1)
    id_count = models.IntegerField('id数', default=1)
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='return_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '回国单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']


# 库存收货单
class InventoryReceiveOrder(BaseModel):
    order_count = models.IntegerField('订单数', default=1)
    box_count = models.IntegerField('箱数', default=1)
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='inventory_receive_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '退换货单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']


# 回国维修收货
class RepairOrder(BaseModel):
    order_count = models.IntegerField('订单数', default=1)
    box_count = models.IntegerField('箱数', default=1)
    id_count = models.IntegerField('id数', default=1)
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        verbose_name='物流员',
        related_name='repair_orders',
        null=True,
        blank=True
    )

    class Meta:
        verbose_name = '维修单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']


# 发货统计
# 未处理订单
class UnprocessedOrder(BaseModel):
    json_data = models.JSONField('数据', default=dict)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, verbose_name='物流员', related_name='unprocessed_orders', null=True, blank=True)

    class Meta:
        verbose_name = '未处理订单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']

    def __str__(self):
        return f"{self.created_by.username if self.created_by else 'Unknown'} - {self.create_time.date()}"


# 运费配置模型
class ShippingFeeConfig(BaseModel):
    """运费配置模型"""
    CARRIER_CHOICES = [
        ('YAMATO', '黑猫'),
        ('SAGAWA', '佐川'),
    ]
    
    carrier = models.CharField('物流公司', max_length=20, choices=CARRIER_CHOICES)
    volume = models.IntegerField('体积(cm³)', help_text='包裹体积')
    region = models.CharField('地区', max_length=50, help_text='配送地区')
    price = models.DecimalField('运费(日元)', max_digits=8, decimal_places=0)
    is_active = models.BooleanField('是否启用', default=True)
    
    class Meta:
        verbose_name = '运费配置'
        verbose_name_plural = '运费配置'
        unique_together = ['carrier', 'volume', 'region']
        ordering = ['carrier', 'volume', 'region']
    
    def __str__(self):
        return f"{self.get_carrier_display()} - {self.volume}cm³ - {self.region} - ¥{self.price}"


# 订单问答模型
class OrderQA(BaseModel):
    """订单问答模型"""
    TIME_SLOT_CHOICES = [
        ('09:30-10:00', '9:30-10:00'),
        ('11:30-12:00', '11:30-12:00'),
        ('13:30-14:00', '13:30-14:00'),
        ('15:30-16:00', '15:30-16:00'),
        ('17:30-18:00', '17:30-18:00'),
    ]
    
    # 基本信息
    question_no = models.CharField('提问单号', max_length=20)
    customer_info = models.CharField('客户/订单信息', max_length=255, blank=True, null=True, default='')
    question_summary = models.CharField('问答概要', max_length=500, blank=True, null=True, default='')
    sales_person = models.CharField('销售', max_length=100, blank=True, null=True, default='')
    time_slot = models.CharField('时间段', max_length=20, choices=TIME_SLOT_CHOICES)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, verbose_name='用户', related_name='order_qas', null=True, blank=True)
    
    class Meta:
        verbose_name = '订单问答'
        verbose_name_plural = '订单问答'
        ordering = ['-create_time']
    
    def __str__(self):
        return f"{self.question_no} - {self.get_time_slot_display()}"
